package br.org.isvi.interpretador.portugol.access;

import java.util.HashMap;
import java.util.List;

import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.query.SelectQuery;

import br.org.isvi.interpretador.portugol.model.Aluno;
import br.org.isvi.interpretador.portugol.model.Aula;
import br.org.isvi.interpretador.portugol.model.Exercicio;
import br.org.isvi.interpretador.portugol.model.SolucaoAluno;
import br.org.isvi.interpretador.portugol.model.Turma;

public class SolucaoAlunoDAO extends AbstractDAO<SolucaoAluno> {

	@Override
	public Class<SolucaoAluno> getType() {
		return SolucaoAluno.class;
	}

	@SuppressWarnings("unchecked")
	public SolucaoAluno obterPorAlunoExercicioAula(Aluno al, Exercicio ex, Aula au) {
		List<SolucaoAluno> ret;
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("alunoId", al.getId());
		map.put("exercicioId", ex.getId());
		map.put("aulaId", au.getId());
		
		SelectQuery query = new SelectQuery(SolucaoAluno.class, Expression.fromString("toAluno = $alunoId and  toAula = $aulaId and toExercicio = $exercicioId").
				expWithParameters(map));
		
		ret = getCtx().performQuery(query);
		
		return ret.size()>0? ret.get(0):null;
	}
	
	@SuppressWarnings("unchecked")
	public List<SolucaoAluno> obterSolucaoAlunoPorTurma(Turma turma) {
		List<SolucaoAluno> ret;
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("turmaId", turma.getId());
		
		SelectQuery query = new SelectQuery(SolucaoAluno.class, Expression.fromString("toAluno.toTurma = $turmaId").
				expWithParameters(map));
		
		ret = getCtx().performQuery(query);
		
		return ret;
	}
	
	
	public static void main(String[] args) {
		List<SolucaoAluno> ret;
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("alunoId", 200);
		map.put("exercicioId", 240);
		map.put("aulaId", 200);
		
		SelectQuery query = new SelectQuery(SolucaoAluno.class, Expression.fromString("toAluno = $alunoId and  toAula = $aulaId and toExercicio = $exercicioId").
				expWithParameters(map));
		
		ret = getCtx().performQuery(query);
		
		System.out.println();
	}
	
}
